package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.sgb.link.api.CommonCallback;
import com.sgb.link.api.CommonNet;
import com.sgb.link.api.CommonThrowable;
import com.sgb.link.entity.merchant.FileEntity;
import com.sgb.link.entity.merchant.InvestmentEntity;
import com.sgb.merchant.api.MerchantsNet;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class ProjectCommitViewModel extends BaseViewModel {
    public MutableLiveData<FileEntity> ldUrl = new MutableLiveData<>();
    InvestmentEntity investmentEntity;
    public MutableLiveData<Boolean> ldResult = new MutableLiveData<>();
    public MutableLiveData<BaseEntity> ldFail = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldState = new MutableLiveData<>();

    public ProjectCommitViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void uploadPic(File file, int pos) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);
        CommonNet.getInstance().getService().uploadFile(photos).enqueue(new CommonCallback<BaseEntity>(false) {
            @Override
            protected void response(BaseEntity data) {
                if (data.isSuccess()) {
                    FileEntity img = new FileEntity();
                    img.setPosition(pos);
                    img.setImgUrl(data.getData().toString());
                    ldUrl.postValue(img);
                }
            }

            @Override
            protected void failure(Throwable t) {
                setFailInfo(t);
            }
        });
    }

    public void publishProject(InvestmentEntity entity) {
        investmentEntity = entity;
        Map<String, Object> map = new HashMap<>();
        if (!TextUtils.isEmpty(investmentEntity.getItemNo())) {
            map.put("itemNo", investmentEntity.getItemNo());
        }
        if (investmentEntity.getImages() != null) {
            map.put("images", investmentEntity.getImages());
        }
        map.put("capacityScaleNo", investmentEntity.getCapacityScaleNo());
        map.put("stageNo", investmentEntity.getStageNo());
        map.put("addressInfo", investmentEntity.getAddressInfo());
        map.put("cityId", investmentEntity.getCityId());
        map.put("proviceId", investmentEntity.getProviceId());
        map.put("regionId", investmentEntity.getRegionId());
        map.put("lng", investmentEntity.getLng());
        map.put("lat", investmentEntity.getLat());
        map.put("itemName", investmentEntity.getItemName());
        map.put("itemInfo", investmentEntity.getItemInfo());
        map.put("itemMoney", investmentEntity.getItemMoney());
        map.put("linkName", investmentEntity.getLinkName());
        map.put("linkPhone", investmentEntity.getLinkPhone());
        //  map.put("status", investmentEntity.getStatus());

        map.put("industryCategoryFirstNo", investmentEntity.getIndustryCategoryFirstNo());
        map.put("industryCategorySecondNo", investmentEntity.getIndustryCategorySecondNo());
        map.put("industryCategoryThreeNo", investmentEntity.getIndustryCategoryThreeNo());
        map.put("industryCategoryFourNo", investmentEntity.getIndustryCategoryFourNo());

        if (!TextUtils.isEmpty(investmentEntity.getLandDiscountContent())) {
            map.put("landDiscountContent", investmentEntity.getLandDiscountContent());
        }
        if (investmentEntity.getLandDiscountNo() != 0) {
            map.put("landDiscountNo", investmentEntity.getLandDiscountNo());
        }
        if (!TextUtils.isEmpty(investmentEntity.getTaxDiscountContent())) {
            map.put("taxDiscountContent", investmentEntity.getTaxDiscountContent());
        }
        if (investmentEntity.getTaxDiscountNo() != 0) {
            map.put("taxDiscountNo", investmentEntity.getTaxDiscountNo());
        }
        MerchantsNet.getInstance().getService().editProject(map).enqueue(new CommonCallback<BaseEntity>(false) {
            @Override
            protected void response(BaseEntity data) {
                if (data.isSuccess()) {
                    investmentEntity.setItemNo(data.getData().toString());
                    publishDemandInfo(investmentEntity);
                }
            }


            @Override
            protected void failure(Throwable t) {
                setFailInfo(t);
            }

        });
    }

    private void setFailInfo(Throwable t) {
        CommonThrowable throwable = null;
        if (t instanceof CommonThrowable) {
            throwable = (CommonThrowable) t;
        }
        if (throwable == null) return;
        BaseEntity entity = throwable.getEntity();
        ldFail.postValue(entity);
    }

    public void publishDemandInfo(InvestmentEntity entity) {
        MerchantsNet.getInstance().getService().editProjectInfo(entity).enqueue(new CommonCallback<BaseEntity>(false) {
            @Override
            protected void response(BaseEntity data) {
                if (data.isSuccess()) {
                    ldResult.postValue(true);
                }
            }

            @Override
            protected void failure(Throwable t) {
                setFailInfo(t);
            }
        });

    }

    public void getCurrentState(long no) {
        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new CommonCallback<BaseEntity<Integer>>(false) {

            @Override
            protected void response(BaseEntity<Integer> data) {
                if (data.isSuccess()) {//0已下架 1已删除 2已上架
                    if (data.getData() != 0) {//若已上架或已删除，则弹出弹窗，关闭弹窗后跳转至投资项目管理页面
                        ldResult.postValue(false);
                    } else {
                        ldState.postValue(true);
                    }
                }
            }

            @Override
            protected void failure(Throwable t) {
                setFailInfo(t);
            }
        });
    }
}
